package www.wen.com.common.view.compose.loading

import android.content.res.Configuration.UI_MODE_NIGHT_YES
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Constraints

@Composable
fun PagePlaceholderLoading(
    pageStatus: PageStatus = PageStatus.LOADING,
    modifier: Modifier,
    content: @Composable () -> Unit,
    loadingPlaceholder: @Composable () -> Unit,
    emptyPlaceholder: @Composable () -> Unit,
    errorPlaceholder: @Composable () -> Unit,
) {
    val contentNew = when (pageStatus) {
        PageStatus.LOADING -> loadingPlaceholder
        PageStatus.SUCCESS -> content
        PageStatus.FAIL -> errorPlaceholder
        PageStatus.EMPTY -> emptyPlaceholder
    }
    Layout(
        content = contentNew,
        modifier = modifier
    ) { measurables, constraints ->
        val measurable = measurables.single()
        val placeable = measurable.measure(Constraints.fixed(constraints.maxWidth, constraints.maxHeight))
        // 设置 自定义 Layout 的宽高
        layout(constraints.maxWidth, constraints.maxHeight) {
            placeable.placeRelative(
                0,
                0
            )
        }
    }
}

@Preview(showBackground = true)
@Preview(
    "Home list drawer screen (dark)",
    showBackground = true,
    uiMode = UI_MODE_NIGHT_YES)
@Composable
fun PreviewPagePlaceholderLoading() {
    PagePlaceholderLoading(
        pageStatus = PageStatus.LOADING,
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight(),
        content = @Composable {

        },
        loadingPlaceholder = @Composable {
            Text("ZXASEED")
        },
        emptyPlaceholder = @Composable {

        },
        errorPlaceholder = @Composable {

        },
    )
}

enum class PageStatus {
    LOADING, SUCCESS, FAIL, EMPTY
}